package com.LeeCode;

/**
 * 统计 X 和 Y 频数相等的子矩阵数量
 */

public class Code3212 {
    public static void main(String[] args) {

    }

    public int numberOfSubmatrices(char[][] grid) {
        int m = grid.length, n = grid[0].length;
        int[][] preX = new int[m + 1][n + 1];
        int[][] preY = new int[m + 1][n + 1];
        int ans = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int numX = (grid[i][j] == 'X' ? 1 : 0) + preX[i][j + 1] + preX[i + 1][j] - preX[i][j];
                int numY = (grid[i][j] == 'Y' ? 1 : 0) + preY[i][j + 1] + preY[i + 1][j] - preY[i][j];
                preX[i + 1][j + 1] = numX;
                preY[i + 1][j + 1] = numY;
                if (numX > 0 && numX == numY) {
                    ans++;
                }
            }
        }
        return ans;
    }
}
